package com.github.tzraeq.d2d;

import android.graphics.PixelFormat;
import android.support.annotation.Nullable;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;

import java.util.Map;

import cn.anxin.a9.avplayer.AVCamera;
import cn.anxin.a9.avplayer.LiveViewGLMonitor;

public class RND2dPlayerManager extends SimpleViewManager<LiveViewGLMonitor> {

    public static final String REACT_CLASS = "RND2dPlayer";

    private LiveViewGLMonitor mMonitor;
    private AVCamera camera;
    private PlayerEventEmitter eventEmitter;

    private boolean hasVideoOut = false;
    private boolean playing = false;
    private boolean stopped = false;

    RND2dPlayerManager() {
        camera = AVCamera.getInstance();
        camera.setEventListener(mPlayerListener);
    }

    @Override
    public String getName() {
        return REACT_CLASS;
    }

    @Override
    protected LiveViewGLMonitor createViewInstance(ThemedReactContext reactContext) {
        mMonitor = new LiveViewGLMonitor(reactContext,camera);
        //默认的zorder, 会导致rn的背景色盖住这个view
//        mMonitor.setZOrderOnTop(true);
        mMonitor.setZOrderMediaOverlay(true);
        mMonitor.getHolder().setFormat(PixelFormat.TRANSLUCENT);//背景透明
//        camera.setMonitor(mMonitor);
//        camera.play();
        return mMonitor;
    }

    @Override
    protected void addEventEmitters(ThemedReactContext reactContext, LiveViewGLMonitor view) {
        super.addEventEmitters(reactContext, view);
        // if(null == eventEmitter){
            eventEmitter = new PlayerEventEmitter(reactContext);
        // }
    }

    @Override
    protected void onAfterUpdateTransaction(LiveViewGLMonitor view) {
        super.onAfterUpdateTransaction(view);
        eventEmitter.setViewId(view.getId());
    }

    public @Nullable
    Map<String, Object> getExportedCustomDirectEventTypeConstants() {
        MapBuilder.Builder<String, Object> builder = MapBuilder.builder();
        for (String event : PlayerEventEmitter.Events) {
            builder.put(event, MapBuilder.of("registrationName", event));
        }
        return builder.build();
    }

    public AVCamera getCamera(){
        return camera;
    }

    public LiveViewGLMonitor getView() { return mMonitor; }

    private WritableMap getEventMap(){
        WritableMap map = Arguments.createMap();
        map.putBoolean("playing", playing);
        map.putBoolean("stopped", stopped);
        map.putBoolean("hasVideoOut",hasVideoOut);
        return map;
    }

    private AVCamera.EventListener mPlayerListener = new AVCamera.EventListener(){

        @Override
        public void onEvent(AVCamera.Event event) {
            switch (event.type) {
                case AVCamera.Event.Playing:
                    playing = true;
                    hasVideoOut = false;
                    stopped = false;
                    eventEmitter.onPlaying(getEventMap());
                    break;
                case AVCamera.Event.Stopped:
                    playing = false;
                    hasVideoOut = false;
                    stopped = true;
                    eventEmitter.onStopped(getEventMap());
                    break;
                case AVCamera.Event.Vout:
                    hasVideoOut = true;
                    eventEmitter.onVout(getEventMap());
                    break;
                case AVCamera.Event.Scale:
                    WritableMap map = getEventMap();
                    Map params = event.params;
                    map.putDouble("scale", ((Float)params.get("scale")).doubleValue());
                    eventEmitter.onScale(map);
                    break;
            }
        }
    };
}
